package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/11/15 13:39
 * @Description:
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> getPatientList(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> list = baseMapper.selectList(queryWrapper);
        for (Patient patient : list) {

            this.packPatient(patient);

        }
        return list;
    }

    private void packPatient(Patient patient){
        String province = dictFeignClient.getName(Long.parseLong(patient.getProvinceCode()));
        String city = dictFeignClient.getName(Long.parseLong(patient.getCityCode()));
        String district = dictFeignClient.getName(Long.parseLong(patient.getDistrictCode()));
        String address = patient.getAddress();
        String name = dictFeignClient.getName(Long.parseLong(patient.getContactsCertificatesType()));
        patient.getParam().put("fullAddress",province + city + district + address);
        patient.getParam().put("certificatesTypeString",name);
    }

    @Override
    public Patient get(Long id) {
        Patient patient = baseMapper.selectById(id);
        Map<String, Object> param = patient.getParam();
        param.put("provinceString",dictFeignClient.getName(Long.parseLong(patient.getProvinceCode())));
        param.put("cityString",dictFeignClient.getName(Long.parseLong(patient.getCityCode())));
        param.put("districtString",dictFeignClient.getName(Long.parseLong(patient.getDistrictCode())));
        patient.setParam(param);
        return patient;
    }
}
